package com.tiancheng.trade.merchant.controller.admin;


import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.merchant.exception.BusinessException;
import com.tiancheng.trade.merchant.model.Product;
import com.tiancheng.trade.merchant.service.IProductService;
import com.tiancheng.trade.merchant.util.ResultJudgeUtil;
import com.tiancheng.trade.merchant.vo.product.CreateProductVO;
import com.tiancheng.trade.merchant.vo.product.FindProductDetailResponseVO;
import com.tiancheng.trade.merchant.vo.product.FindProductPageListVO;
import com.tiancheng.trade.merchant.vo.product.UpdateProductVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class ProductAdminController {
    @Autowired
    private IProductService productService;

    /**
     * 分页查询商品信息
     *
     * @param productVO {@link FindProductPageListVO}
     * @return {@link Result}<{@link PageInfo}<{@link Product}>>
     */
    @GetMapping("/findPageList")
    public Result<PageInfo<Product>> findPageList(FindProductPageListVO productVO) throws BusinessException {
        return Result.success(productService.findPageList(productVO));
    }

    @GetMapping("/findPaymentChannelByProductCodes")
    public Result<List<Product>> findPaymentChannelByProductCodes(@RequestParam("list") List<String> list)
            throws BusinessException {

        return Result.success(productService.findPaymentChannelByProductCodes(list));
    }

    /**
     * 按业务侧编号查询是否有重复的业务侧产品编号
     *
     * @param productVO {@link FindProductPageListVO}
     * @return {@link Result}<{@link Integer}>
     */
    @GetMapping("/findProductCodeBusiness")
    public Result<Integer> findProductCodeBusiness(FindProductPageListVO productVO) throws BusinessException {
        if (StringUtils.isEmpty(productVO.getProductCodeBusiness())) {
            return Result.success(0);
        }
        List<Product> list = productService.findProductCodeBusiness(productVO);
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.success(1);
        }
        return Result.success(0);
    }

    @GetMapping("/{id}")
    public Result<FindProductDetailResponseVO> findDetail(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(productService.findDetailById(id));
    }

    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return ResultJudgeUtil.judge(productService.removeById(id), "删除");
    }


    /**
     * 修改
     *
     * @param productVO {@link UpdateProductVO}
     */
    @PutMapping("/")
    public Result<?> update(@RequestBody UpdateProductVO productVO) throws BusinessException {
        Assert.isTrue(productVO.getId() != null, "参数错误~");
        FindProductPageListVO product = new FindProductPageListVO();
        product.setId(productVO.getId());
        product.setProductCodeBusiness(productVO.getProductCodeBusiness());
        List<Product> list = productService.findProductCodeBusiness(product);
        Assert.isTrue(list != null, "业务侧编号已存在~");
        if (CollectionUtils.isNotEmpty(list)) {
            return ResultJudgeUtil.judge(0, "业务侧编号已存在~");
        }
        return ResultJudgeUtil.judge(productService.update(productVO), "修改");
    }


    /**
     * 新增
     *
     * @param productVO {@link CreateProductVO}
     */
    @PostMapping("/")
    public Result<?> create(@RequestBody CreateProductVO productVO) throws BusinessException {
        FindProductPageListVO product = new FindProductPageListVO();
        product.setProductCodeBusiness(productVO.getProductCodeBusiness());
        List<Product> list = productService.findProductCodeBusiness(product);
        Assert.isTrue(list != null, "业务侧编号已存在~");
        if (CollectionUtils.isNotEmpty(list)) {
            return ResultJudgeUtil.judge(0, "业务侧编号已存在~");
        }
        return ResultJudgeUtil.judge(productService.create(productVO), "新增");
    }


    /**
     * 查询清分规则列表信息
     *
     * @param merchantVO {@link FindProductPageListVO}
     */
    @PostMapping("/export")
    public void productExport(@RequestBody FindProductPageListVO merchantVO, HttpServletRequest request,
                              HttpServletResponse response) throws IOException {

        productService.export(merchantVO, request, response);
    }

    /**
     * 删除商品，从数据库删除，不做软删除
     * @param productCode
     * @return
     * @throws BusinessException
     */
    @DeleteMapping("/deleteByProductCode/{productCode}")
    public Result<?> deleteByProductCode(@PathVariable("productCode") String productCode) throws BusinessException {
        Assert.isTrue(!StringUtils.isEmpty(productCode), "参数错误~");
        return ResultJudgeUtil.judge(productService.deleteByProductCode(productCode), "删除");
    }
}
